题目链接:http://codeforces.com/problemset/problem/558/C

 

题意:把n个数变成相同所需要走的最小的步数
易得到结论,两个奇数不同,一直×2不可能有重叠
枚举每个数可能到得所有值,以及统计达到该值的时候已经走的步数
最终答案就是1到up中num[i]最小的数

 

Examples
input
3
4 8 2
output
2

input 3 3 5 6 output 5

Note In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4. In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1.

 

***********************************************************

 

AC代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<iostream>
 6 #include<queue>
 7 #include<map>
 8 #include<stdlib.h>
 9 
10 using namespace std;
11 
12 #define N 5200
13 #define INF 0x3f3f3f3f
14 #define Maxn 100010
15 
16 int a[Maxn],step[Maxn],num[Maxn];
17 
18 int main()
19 {
20     int n,i;
21 
22     while(scanf("%d", &n) != EOF)
23     {
24         memset(step,0,sizeof(step));
25         memset(num,0,sizeof(num));
26 
27         for(i=1; i<=n; i++)
28             scanf("%d", &a[i]);
29 
30         for(i=1; i<=n; i++)
31         {
32             int x=a[i],sc=0;
33 
34             while(x)
35             {
36                 int s1=0;
37                 while(x%2==0)
38                 {
39                     x/=2;
40                     s1++;
41                 }
42 
43                 int y=x,s2=0;
44                 while(y<=Maxn)
45                 {
46                     num[y]++;
47                     step[y]+=sc+abs(s1-s2);
48                     s2++;
49                     y*=2;
50                 }
51 
52                 sc+=s1+1;
53                 x/=2;
54             }
55         }
56         int ans=INF;
57         for(i=1;i<=Maxn;i++)
58             if(num[i]==n)
59             ans=min(ans,step[i]);
60 
61         printf("%d\n", ans);
62     }
63     return 0;
64 }

 

这个里写的很清楚:http://blog.csdn.net/u014028317/article/details/46897963

posted on 2016-08-12 16:07  惟愿。。。  阅读(358)  评论(0编辑  收藏  举报